﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using Ashlen.Portal.Core;
using Ashlen.Portal.Core.DTO;
using Ashlen.Portal.Core.DataInterfaces;
using Ashlen.Portal.Data.SqlServer;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Caching;

namespace Ashlen.Portal.Services
{
    // NOTE: If you change the class name "AdministrationService" here, you must also update the reference to "AdministrationService" in Web.config.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AdministrationService : ServiceBase, IAdministrationService
    {
        private readonly IAdministrativeDao _adminDao;
        private readonly IUtilityDao _utility;

        public AdministrationService()
        {
            _adminDao = new AdministrationDao();
            _utility = new UtilityDao();
        }

        public AdministrationService(string connectionString)
        {
            _adminDao = new AdministrationDao(connectionString);
            _utility = new UtilityDao(connectionString);
        }

        public string GetRoles()
        {
             ServiceResponse response = new ServiceResponse();
             try
             {
                 base.SetCache();
                 response.IsSuccess = true;
                 response.Payload = _adminDao.GetRoles();
             }
             catch (Exception ex)
             {
                 response = Utility.GetErrorResponse(ex);
             }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string SaveRole(string roleId, string roleName, string description)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                Guid? gRoleId = (roleId == null || roleId.Trim().Equals(string.Empty)) ? new Nullable<Guid>() : new Guid(roleId);
                _adminDao.SaveRole(Constants.APPLICATION, gRoleId, roleName, description);
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string DeleteRole(string roleId)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                _adminDao.DeleteRole(new Guid(roleId));
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }


        public string GetUsers(string partialUserName)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                response.Payload = _adminDao.GetUsers(partialUserName);
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string GetUsersWithRoles(string partialUserName)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                response.Payload = _adminDao.GetUsersWithRoles(partialUserName);
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string DeleteUser(string userId)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                _adminDao.DeleteUser(new  Guid(userId));
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string AddUserToRole(string userId, string roleId)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                _adminDao.AddUserToRole(new Guid(userId), new Guid(roleId));
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string DeleteUserFromRole(string userId, string roleId)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {
                base.SetCache();
                _adminDao.DeleteUserFromRole(new Guid(userId), new Guid(roleId));
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }
    }
}
